package cc.wsyw126.java.interview.jingdong;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


//()
//number = 1
//((()))
//number = 6
//(((())))
//number = 24
//((((()))))
//number = 120
//将字符串经过移除操作变为 空串。1. 删除左边（ 2.移除右边) 剩余串为合法串，左边（大于等于右边）个数


/**
 * Created by Administrator on 2017/9/6.
 */
public class Main1 {
    private static Map<String, Integer> map = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            StringBuilder stringBuilder = new StringBuilder(s);
            int number = findNumber(stringBuilder);
            System.out.println("number = " + number);
        }
    }

    private static int findNumber(StringBuilder builder) {
        if (builder.toString().equals("()")) {
            return 1;
        }
        if (map.get(builder.toString()) != null) {
            return map.get(builder.toString());
        }
        int count = 0;
        StringBuilder stringBuilder = new StringBuilder(builder).deleteCharAt(0);
        for (int i = 0; i < stringBuilder.length(); i++) {
            if (stringBuilder.charAt(i) == ')') {
                StringBuilder stringBuilder1 = new StringBuilder(stringBuilder).deleteCharAt(i);
                int left = 0, right = 0;
                boolean illegal = true;
                for (int j = 0; j < stringBuilder1.length(); j++) {
                    if (stringBuilder1.charAt(j) == '(') {
                        left++;
                    } else {
                        right++;
                    }
                    if (right > left) {
                        illegal = false;
                        break;
                    }
                }
                if (illegal) {
                    count += findNumber(stringBuilder1);
                }
            }
        }
        map.put(builder.toString(), count);
        return count;
    }


}
